import { message } from 'antd';

export default {
  namespace: 'es',

  state: {
    eapMsg:[], // 界面上要显示的Eap通信消息内容
    cimMessage:"消息为空",// 远程消息提示内容
    cimMessageVisible:false, // 远程消息提示框是否显示
    userIdserver:"当前无用户使用", // 上下岗的用户Id
    userLogin:false, // 用户是否上岗
    loginHintVisible:false, // 员工上下岗提示框
    loginHint:"无信息", // 上下岗提示信息
    serverEapParams:[], // 数据库中的关于和EAP通信的一些参数.
    eapConnect:false, // eap是否连接上,默认是false
  },

  effects: {

    *handleCimMsgState({payload},{call,put }){
      const {msg} = payload;
      console.log("in handleCimMsgState: " + msg.val);
      if(msg.val == "updateCimMessage"){
        yield put({
          type:"updateCimMessageInfo",
          payload:[msg.visible,msg.cimMessage],
        });
      
      }
      },

    *handleEapMsgState({payload},{call,put }){
      const {msg} = payload;
      if(msg.val == "updateEapMsg"){
        yield put({
          type:"updateEapMsg",
          payload:msg.info,
        });
      
      }else if (msg.val == "updateEapParams"){
        yield put({
          type:"updateEapParams",
          payload:msg.newParams,
        });
      }else if (msg.msg.val == "updateUserLogin"){
        yield put({
          type:"updateUserLogin",
          payload:msg.msg.info,
        });
      }else if (msg.val == "updateCimMessage"){
        yield put({
          type:"updateCimMessageInfo",
          payload:[msg.visible,msg.cimMessage],
        });
      }else if (msg.msg.val == "updateLoginReport"){
        yield put({
          type:"updateLoginReport",
          payload:msg.msg.info,
        });
      }
    },
    *keepCimMessage({payload},{call,put}){
      const response = yield call(window.eel.run_py,"es","keep_cim_message",[payload]);
      if (response === "ok"){
        message.success("远程讯息保存成功!",3)
      }else{
        message.error("远程讯息保存失败!, 失败原因: " + response ,3)
      }
    },

    *userLogin({payload},{call,put}){
      const response = yield call(window.eel.run_py,"es","user_login",[]);
      if (response === "ok"){
        message.success("上岗请求发送成功!",3)
      }else{
        message.error("上岗请求发送失败, 失败原因: " + response ,3)
      }
    },

    *userLogout({payload},{call,put}){
      const response = yield call(window.eel.run_py,"es","user_logout",[]);
      if (response === "ok"){
        message.success("下岗请求发送成功!",3)
      }else{
        message.error("下岗请求发送失败, 失败原因: " + response ,3)
      }
    },

    *updateUserId({payload},{call,put}){
      const response = yield call(window.eel.run_py,"es","update_user_id",[payload]);
      if (response[0] === "ok"){
        message.success("更新成功!",3)
        yield put({
          type:"updateUserLogin",
          payload:[response[1],false]
        });
      }else{
        message.error("更新失败, 失败原因: " + response ,3)
      }
    },

    *connectEap({ payload }, { call, put }) {
      const response = yield call(window.eel.run_py, 'es', 'connect_eap', []);
      if (response === "ok"){
        message.success("连接成功!",3)
        yield put({
          type:'updateEapConnect',
          payload:true,
        });
      }else {
        message.error("连接失败! 失败原因:" + response,3);
        yield put({
          type:'updateEapConnect',
          payload:false
        })
      }
    },

    *clearMsgshow({payload},{call,put}){
      const response = yield call(window.eel.run_py,'es','clear_msgshow',[])
      if (response === "ok"){
        message.success("清空成功",3)
      }else{
        message.error("清空失败,失败原因: "+response,3);
      }
    },

    *disconnectEap({ payload }, { call, put }) {
      const response = yield call(window.eel.run_py, 'es', 'disconnect_eap', []);
      if (response === "ok"){
        message.success("断开EAP连接成功!",3)
        yield put({
          type:'updateEapConnect',
          payload:false,
        });
      }else {
        message.error("断开EAP连接失败! 失败原因:" + response,3);
      }
    },


    *getEapParams({ payload }, { call, put }) {
      const params = yield call(window.eel.run_py, 'es', 'get_eap_params', []);
      yield put({
        type: 'updateEapParams',
        payload: params,
      });
    },

    *saveEapParams({ payload }, { call, put }) {
      const response = yield call(window.eel.run_py, 'es', 'save_eap_params', [payload]);
      if (response == "ok"){
        message.success("保存成功",3);
      }else{
        message.error("保存失败,失败原因: ",response);
      }
      yield put ({
        type:'getEapParams'
      });
    },
  },
  reducers:{

    closeCimMessageModel(state, action) {
      return {
        ...state,
        cimMessageVisible: false,
        cimMessage:"",
      };
    },
    updateLoginReport(state, action) {
      return {
        ...state,
        loginHintVisible: true,
        loginHint:action.payload,
      };
    },
    updateCimMessageInfo(state, action) {
      return {
        ...state,
        cimMessageVisible: action.payload[0],
        cimMessage:action.payload[1],
      };
    },
    updateUserLogin(state,action){
      return {
        ...state,
        userIdserver:action.payload[0],
        userLogin:action.payload[1],
      }
    },
    updateEapMsg(state,action){
      return {
        ...state,
        eapMsg:action.payload,
      }
    },
    updateEapConnect(state,action){
      return{
        ...state,
        eapConnect:action.payload,
      };
    },
    updateEapParams(state,action){
      return{
        ...state,
        serverEapParams:action.payload,
      };
    },

    addServerEapParams(state, action) {
      return {
        ...state,
        serverEapParams: action.payload,
      };
    },
  },
};
